<template>
  <div>
    <!-- 新增商品 -->
    
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item prop="img" label="商品图片">
        <el-upload class="avatar-uploader" name="sbb" :action="`${UPLOADURL}/upload`" :show-file-list="false"
          :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="ruleForm.img" :src="ruleForm.img" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="商品名称" prop="product_name">
        <el-input style="width: 400px;" v-model="ruleForm.product_name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input style="width:  400px;" v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="商品规格" prop="specifications">
        <el-input style="width:  400px;" v-model="ruleForm.specifications"></el-input>
      </el-form-item>
      <el-form-item label="商品描述" prop="Description">
        <el-input style="width:  400px;" v-model="ruleForm.Description"></el-input>
      </el-form-item>
      <el-form-item label="商品颜色" prop="pro_color">
        <el-input style="width:  400px;" v-model="ruleForm.pro_color"></el-input>
      </el-form-item>


      <el-form-item label="已售多少" prop="sales">
        <el-input style="width:  400px;" v-model="ruleForm.sales"></el-input>
      </el-form-item>

      <el-form-item label="商品分类" prop="category_id">
        <el-radio-group style="width:  400px;" v-model="ruleForm.category_id">
          <el-radio :value="1" label="女子服饰"></el-radio>
          <el-radio :value="2" label="男子服饰"></el-radio>
          <el-radio :value="3" label="家用智能"></el-radio>
          <el-radio :value="4" label="身体数据"></el-radio>
          <el-radio :value="5" label="运动装备"></el-radio>
          <el-radio :value="6" label="潮鞋"></el-radio>
          <el-radio :value="7" label="运动护具"></el-radio>
          <el-radio :value="8" label="健康食品"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from '@/http';
export default {

  data() {



    return {
      ruleForm: {
        img: '',
        product_name: '',
        price: '',
        sales: '',
        category_id: '',
        specifications: '',
        Description: '',
        pro_color: ''
      },

      rules: {
        img:[
        { required: true, message: '必填', trigger: 'blur' },
        ],
        product_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
        ],
        sales: [
          { required: true, message: '必填', trigger: 'blur' },
        ],
        category_id: [
          { required: true, message: '请选择分类', trigger: 'change' }
        ],
        specifications: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        Description: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        pro_color: [
          { required: true, message: '必填', trigger: 'change' }
        ],
      }
    };
  },

  methods: {

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          httpApi.mallApi.mallAdd(this.ruleForm).then(res => {
            console.log('新增的商品', res);
          })
          alert('添加成功!');
          this.$router.push('/home/product-list')
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },




    handleAvatarSuccess(res, file) {
      this.ruleForm.img = res.pic
      console.log(res);
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}

</script>

<style >
.el-input {
  width: 200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  display: block;
}
</style>